<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;

/**
 * 用户冻结管理
 *
 * @icon fa fa-circle-o
 */
class Membefreeze extends Backend
{

    /**
     * Membefreeze模型对象
     * @var \app\admin\model\Membefreeze
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Membefreeze;

    }


       /**
     * 查看
     */
    public function index($ids=null)
    {

        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            
            $where_arr["uid"] = $ids;
            
            
        
            $list = $this->model
                    ->where($where)
                    ->where($where_arr)
                    ->order($sort, $order)
                    ->paginate($limit);


            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        $this->view->assign("zid",$ids);
        return $this->view->fetch();
    }
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

     //编辑
    public function add($ids = null)
    {
  
        $zid = input("zid");
       
       
       if (false === $this->request->isPost()) {
            $this->view->assign('zid', $zid);
            return $this->view->fetch();
        }
        
        
        $params = $this->request->post('row/a');
        $result = false;
        Db::startTrans();
        try {
                if(strtotime($params["end_time"]) < time()+300){
                    $this->error("结束时间必须大于五分钟");
                }
                
                
                $che_id = explode(",",$params["str_val"]);    
                $validate = [];
                $arr = ["动态","聊天","橱窗","抢单权限","发单权限","下单权限","提现权限","充值权限","冻结昵称","冻结头像","冻结个性签名"];
                
                foreach ($arr as $k=>$v){
                    
                    if(!empty($che_id)){
                        if(in_array($k,$che_id)){
                            $z = [];
                            $validate[] = $v;
                            // db("weigui")->insert(["title"=>$v,"end_time"=>$params["end_time"],"uid"=>$zid,"ids"=>$k,"content"=>$params["reason_msg"],"flag"=>"已冻结"]);
                        }
                    }
                }
          
            $params["uid"] = $zid;
            $params["str_val_text"]  = implode(",",$validate);
        
            $params["dream_id"] = db("membe")->where("membe_id",$zid)->value("dream_id");;
            $result = db("membe_freeze")->insertGetId($params);
            
         
         
              foreach ($arr as $k=>$v){
                    
                    if(!empty($che_id)){
                        if(in_array($k,$che_id)){
                          
                            db("weigui")->insert(["title"=>$v,"end_time"=>$params["end_time"],"uid"=>$zid,"ids"=>$result,"content"=>$params["reason_msg"],"flag"=>"已冻结"]);
                        }
                    }
                }
            
            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if (false === $result) {
            $this->error(__('No rows were updated'));
        }
        $this->success();
    }
    
    
     public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
            $this->error(__('You have no permission'));
        }
        if (false === $this->request->isPost()) {
            $this->view->assign('row', $row);
            return $this->view->fetch();
        }
        $params = $this->request->post('row/a');
        if (empty($params)) {
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $params = $this->preExcludeFields($params);
        $result = false;
        Db::startTrans();
        try {
            
    
           $che_id = explode(",",$params["str_val"]);    
                $validate = [];
                $arr = ["动态","聊天","橱窗","抢单权限","发单权限","下单权限","提现权限","充值权限","冻结昵称","冻结头像","冻结个性签名"];
                
                foreach ($arr as $k=>$v){
                    
                    if(!empty($che_id)){
                        if(in_array($k,$che_id)){
                            $z = [];
                 
                            $validate[] = $v;
                            
                            
                            
                        }
                     
                    }
                  
                }
          
              $params["str_val_text"]  = implode(",",$validate);
            $result = $row->allowField(true)->save($params);
            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if (false === $result) {
            $this->error(__('No rows were updated'));
        }
        $this->success();
    }
}
